import setting from '@/config/setting'

const getStorageName = name => !!setting.storagePrefix ? `${setting.storagePrefix}_${name}` : name

export default {
  /**
   * 存储localStorage
   */
  set (name, content) {
    if (!name) return
    if (typeof content !== 'string') {
      content = JSON.stringify(content)
    }
    window.localStorage.setItem(getStorageName(name), content)
  },

  /**
   * 获取localStorage
   */
  get (name, def = {}) {
    if (!name) return
    let content = window.localStorage.getItem(getStorageName(name))
    try {
      return JSON.parse(content) || def
    } catch (err) {
      return content
    }
  },

  /**
   * 删除localStorage
   */
  remove (name) {
    if (!name) return
    window.localStorage.removeItem(getStorageName(name))
  }
}
